package com.example.everying.controller.pay.wx;

import com.alibaba.fastjson2.JSONObject;
import com.example.everying.annotation.WebLog;
import com.example.everying.framework.pay.service.WechatTransferService;
import com.example.everying.model.dto.pay.PaymentOrderQueryDto;
import com.example.everying.model.dto.pay.TransactionDto;
import com.example.everying.model.dto.pay.TransferBatchEntityDto;
import com.example.everying.model.dto.pay.TransferCallbackResult;
import com.example.everying.model.entity.pay.PaymentOrderEntity;
import com.example.everying.service.pay.PaymentOrderService;
import com.example.everying.service.pay.TransferBatchService;
import com.example.everying.utils.Result;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Api(tags = "微信支付")
@RequestMapping("/pay/wx")
@RequiredArgsConstructor
public class WxController {

    @Autowired
    private PaymentOrderService paymentOrderService;
    @Autowired
    private TransferBatchService transferBatchService;
    @Autowired
    private WechatTransferService wechatTransferService;

    @ApiOperation(value = "充值")
    @PostMapping("recharge")
    @WebLog(description = "充值")
    public Result<?> recharge(@ApiParam(name = "PaymentOrderEntity", value = "充值", required = true)
                              @RequestBody PaymentOrderEntity entity) {
        return paymentOrderService.createOrder(entity);
    }

    @ApiOperation(value = "查询订单")
    @PostMapping("queryOrder")
    @WebLog(description = "查询订单")
    public Result<?> queryOrder(@ApiParam(name = "PaymentOrderQueryDto", value = "订单号", required = true)
                                @RequestBody PaymentOrderQueryDto dto) {
        return paymentOrderService.queryOrder(dto.getOutTradeNo());
    }

    @ApiOperation(value = "回调")
    @PostMapping("payCallback")
    @WebLog(description = "回调")
    public Result<?> payCallback(HttpServletRequest request) throws IOException {
        return paymentOrderService.payCallback(request);
//    }
//    @ApiOperation(value = "回调")
//    @PostMapping("payCallback")
//    @WebLog(description = "回调")
//    public Result<?> payCallback(@ApiParam(name = "TransactionDto", value = "订单号", required = true )
//                                     @RequestBody TransactionDto TransactionDto) throws IOException {
//        return paymentOrderService.payCallback(TransactionDto);
    }

    @ApiOperation(value = "回调")
    @PostMapping("transferCallback")
    @WebLog(description = "回调")
    public JSONObject transferCallback(HttpServletRequest request) throws IOException {
        return transferBatchService.transferCallback(request);
    }


    //    @ApiOperation(value = "回调2")
//    @PostMapping("transferCallback2")
//    @WebLog(description = "回调")
//    public JSONObject transferCallback(@ApiParam(name = "TransferBatchEntityDto", value = "提现", required = true)
//                                           @RequestBody TransferCallbackResult request) throws IOException {
//        return transferBatchService.transferCallback(request);
//    }
    @ApiOperation(value = "提现")
    @PostMapping("transfer")
    @WebLog(description = "提现")
    public Result<?> transfer(@ApiParam(name = "TransferBatchEntityDto", value = "提现", required = true)
                              @RequestBody TransferBatchEntityDto entity) {
        return transferBatchService.createTransferBatch(entity);
    }

    @ApiOperation(value = "提现确认")
    @PostMapping("confirmTransfer")
    @WebLog(description = "提现确认")
    public Result<?> confirmTransfer(@ApiParam(name = "TransferBatchEntityDto", value = "提现", required = true)
                                     @RequestBody TransferBatchEntityDto entity) {
        transferBatchService.confirmTransfer(entity.getBatchNo());
        return Result.ok();
    }
}
